package com.eleadmin.order.controller;

import com.eleadmin.damon.utils.results.PageResult2;
import com.eleadmin.common.core.web.*;
import com.eleadmin.common.core.annotation.OperationLog;
import com.eleadmin.order.entity.Settings;
import com.eleadmin.order.service.SettingsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.ui.Model;


/**
 *
 * 设置项
 * @author : Doman
 * @date : 2023-03-31 11:23:58
 * @version ：1.0
 */

@Api(tags = "设置项")
@RestController
@RequestMapping("/api/order/settings")
public class SettingsController extends BaseController {

    @Autowired
    private SettingsService settingsService;

    @PreAuthorize("hasAuthority('order:settings:list')")
    @ApiOperation("分页查询设置项")
    @GetMapping("/page")
    public JsonResult page(PageResult2<T> page, Settings settings) throws Exception{
        return settingsService.list(page, settings, getLoginUser());
    }

    @PreAuthorize("hasAuthority('order:settings:list')")
    @ApiOperation("查询全部设置项")
    @GetMapping()
    public JsonResult list(Settings settings){
        return settingsService.list(settings, getLoginUser());
    }

    @PreAuthorize("hasAuthority('order:settings:list')")
    @ApiOperation("返回开票select列表")
    @GetMapping("listSel")
    public JsonResult listSel(Settings settings){
        return settingsService.listSel(settings, getLoginUser());
    }

    @PreAuthorize("hasAuthority('order:settings:list')")
    @ApiOperation("根据id查询设置项")
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable("id") String id) {
        return settingsService.getById(id);
    }


    @PreAuthorize("hasAuthority('order:settings:save')")
    @OperationLog
    @ApiOperation(value ="添加设置项")
    @PostMapping()
    public JsonResult save(@RequestBody Settings settings) {
        try {
            return settingsService.save(settings, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }

    }

    @PreAuthorize("hasAuthority('order:settings:update')")
    @OperationLog
    @ApiOperation(value ="修改设置项")
    @PutMapping()
    public JsonResult update(@RequestBody Settings settings) {
        try {
            return settingsService.update(settings, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }

    @PreAuthorize("hasAuthority('order:settings:remove')")
    @OperationLog
    @ApiOperation(value ="删除设置项")
    @DeleteMapping("/{id}")
    public JsonResult remove(@PathVariable("id") String id) {
        try {
            return settingsService.remove(id, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }

}
